package com.miao.common.controller;

import com.alibaba.fastjson.JSON;
import com.miao.admin.service.AdminCustomerService;
import com.miao.common.entity.Message;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Customer;
import com.miao.common.service.ChatService;
import com.miao.common.ws.WebSocketServer;
import com.miao.websecurity.entity.Admin;
import com.miao.websecurity.service.AdminService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * @author MiaoWenqing
 * @description: 客服
 * @date 2021/2/4 17:08
 */
@RestController
@RequestMapping("common/chat")
public class ChatController {


    @Resource
    private ChatService chatService;

    @Resource
    private AdminCustomerService adminCustomerService;

    @Resource
    private WebSocketServer webSocketServer;

    @Resource
    private AdminService adminService;


    /**
     * 客服随机接入一个客户
     * @param storeId 店铺id
     * @param waiter 客服id
     * @return R
     */
    @GetMapping("/accessChat")
    public R accessChat(String storeId,String waiter){
        List<Object> ids = chatService.waitQueue(storeId);
        if(null==ids||ids.size()==0){
            return R.error();
        }
        int index = new Random().nextInt(ids.size() - 1);
        String id = (String) ids.get(index);//随机接入一个客户
        chatService.popWaitQueue(storeId,id);
        Customer customer = this.adminCustomerService.getById(id);
        Admin admin = adminService.getById(waiter);

        //移出等待对列
        chatService.popWaitQueue(storeId,id);


        Message message = new Message();
        message.setType("1");
        message.setMsg("您好，我是客服["+admin.getAdminName()+"] 您已接入人工客服。 有什么可以帮您的吗？");
        message.setStoreId(storeId);
        message.setSendId(waiter);
        message.setReceiveId(id);
        message.setReceiveAvatar(customer.getCusAvatar());

        webSocketServer.sendInfo(id, JSON.toJSONString(message));
        return R.ok(customer);

    }



    @GetMapping("/marchWaiter")
    public R matchWaiter(String storeId,String uId){

        long l = this.chatService.matchWaiter(storeId);
        System.out.println(l);
        if (l<=0){
            //客服不在线
            Customer customer = this.adminCustomerService.getById(uId);
//            Message message = new Message();
//            message.setType(1);
//            message.setMsg("您好，我是客服[人工智能]。 有什么可以帮您的吗？");
//            message.setStoreId(Integer.parseInt(storeId));
//            message.setSendId(99999);
//            message.setReceiveId(Integer.parseInt(uId));
//            message.setReceiveAvatar(customer.getCusAvatar());
//            message.setSendTime(new Date());
            HashMap<String, String> message = new HashMap<>();
            message.put("type","1");
            message.put("msg","您好，我是客服[人工智能]。 有什么可以帮您的吗？");
            message.put("storeId",storeId);
            message.put("sendId","99999");
            message.put("receiveId",uId);
            message.put("receiveAvatar",customer.getCusAvatar());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            message.put("sendTime",simpleDateFormat.format(new Date()));
            System.out.println(message);
            webSocketServer.sendInfo(uId, JSON.toJSONString(message));
            return R.ok("99999");

        }else {
            //有客服在线

            int i = new Random().nextInt((int) l);
            String waiterId = this.chatService.getWaiterId(storeId, i);
            Admin admin = adminService.getById(waiterId);
            Customer customer = this.adminCustomerService.getById(uId);
//            Message message = new Message();
//            message.setType(1);
//            message.setMsg("您好，我是客服["+admin.getAdminName()+"] 您已接入人工客服。 有什么可以帮您的吗？");
//            message.setStoreId(Integer.parseInt(storeId));
//            message.setSendId(Integer.parseInt(waiterId));
//            message.setReceiveId(Integer.parseInt(uId));
//            message.setReceiveAvatar(customer.getCusAvatar());
//            message.setSendTime(new Date());


            HashMap<String, String> message = new HashMap<>();
            message.put("type","1");
            message.put("msg","您好，我是客服[人工]。 有什么可以帮您的吗？");
            message.put("storeId",storeId);
            message.put("sendId","99999");
            message.put("receiveId",uId);
            message.put("receiveAvatar",customer.getCusAvatar());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            message.put("sendTime",simpleDateFormat.format(new Date()));

            webSocketServer.sendInfo(uId, JSON.toJSONString(message));
            chatService.popWaitQueue(storeId,uId);


            return R.ok(admin.getAdminId());
        }


    }

    /**
     * 获取全部当前客户的全部聊天记录
     * @param uid 客户id
     * @param storeId 店铺id
     * @return R
     */
    @GetMapping("/messageRecord")
    public R getMessageRecord(String uid ,String storeId){
        System.out.println("==============================");
        System.out.println(uid+"======"+storeId);
        List<Message> messageRecord = this.chatService.getMessageRecord(uid, storeId);
        return R.ok(messageRecord);

    }



}
